<?php

class Admin_QuestionController extends ExtLb_Controller_Action
{
	private $questionForm;
	private $questionTbl;
	
    public function init()
    {
        $this->loadLayout('admin', 'layout');
        $this->loadCss('admin', 'question');
        $this->questionForm = new Admin_Form_QuestionForm();
    	$this->questionTbl = new Admin_Model_Question();
    }

    public function indexAction()
    {
    	
    	$this->view->questionForm = $this->questionForm;
    	$this->view->titleView = "Thêm câu hỏi mới";
    	
    	//GET DATA TO EDIT QUESTION
    	if(isset($_GET['edit']) && !empty($_GET['id'])){
    		$this->questionForm->addAction('edit');
    		$this->questionForm->addSubmitButton('cmdEdit', 'Cập nhập');
    		
    		$question = $this->questionTbl->fetchQuestion($_GET['id']);
    		
    		$this->questionForm->addHiddenId('txtId', $question['idCauHoi']);
    		$this->questionForm->getElement('txtArName')->setValue($question['tenCauHoi']);
    		$_REQUEST['cbbMonHoc'] = $question['idMonHoc'];
    		$this->questionForm->getElement('txtA')->setValue($question['a']);
    		$this->questionForm->getElement('txtB')->setValue($question['b']);
    		$this->questionForm->getElement('txtC')->setValue($question['c']);
    		$this->questionForm->getElement('txtD')->setValue($question['d']);
    		$this->questionForm->getElement('txtE')->setValue($question['e']);
    		$this->questionForm->getElement('cbbTrueQuestion')->setValue($question['dapAnDung']);
    		$_REQUEST['cbbLevel'] = $question['mucDo'];
    		
    	}else{
    		$this->questionForm->addAction('add');
    		$this->questionForm->addSubmitButton('cmdAdd', 'Thêm');
    		
    	}
    	
    }

    public function addAction()
    {
    	if($this->questionForm->isValid($this->_request->getPost())){
        	if(isset($_POST['cmdAdd'])){
	        	$nameQues = $this->_getParam('txtArName');
	        	$idMon = $this->_getParam('cbbMonHoc');
	        	$a = $this->_getParam('txtA');
	        	$b = $this->_getParam('txtB');
	        	$c = $this->_getParam('txtC');
	        	$d = $this->_getParam('txtD');
	        	$e = $this->_getParam('txtE');
	        	$trueQues = $this->_getParam('cbbTrueQuestion');
	        	$level = $this->_getParam('cbbLevel');
	        	
	        	$this->questionTbl->addQuestion($nameQues, $idMon, $a, $b, $c,$d,$e, $trueQues,$level);
	        	
	        	$this->_request->clearParams();
	        	$this->jShowMessage("Đã thêm!!!");
	        	
	        	$this->_forward('addquestion','quiz','admin');
        	}
        	
        }else{
        	$this->_redirect('admin/question/');
        }
    }

    public function editAction()
    {
    	
        if($this->questionForm->isValid($this->_request->getPost())){
        	if(isset($_POST['cmdEdit'])){
        		$nameQues = $this->_getParam('txtArName');
	        	$idMon = $this->_getParam('cbbMonHoc');
	        	$a = $this->_getParam('txtA');
	        	$b = $this->_getParam('txtB');
	        	$c = $this->_getParam('txtC');
	        	$d = $this->_getParam('txtD');
	        	$e = $this->_getParam('txtE');
	        	$trueQues = $this->_getParam('cbbTrueQuestion');
	        	$level = $this->_getParam('cbbLevel');
	        	$id = $this->_getParam('txtId');
	        	
	        	$this->questionTbl->updateQuestion($nameQues, $idMon, $a, $b, $c, $d, $e, $trueQues,$level, $id);
	        	
	        	$this->jShowMessage("Thành công!!!");
	        	
	        	$this->_forward('addquestion','quiz','admin');
        	}else {
        		$this->_redirect('admin/question/?edit&id='.$_POST['txtId']);	
        	}
        }else{
        	$this->_redirect('admin/question/');
        }
    }

    public function deleteAction()
    {
    	if(isset($_GET['delete']) && !empty($_GET['id'])){
    		$this->questionTbl->deleteQuestion($_GET['id']);
    		
    		$this->jShowMessage("Đã xóa!!!");
    		
    		$this->_redirect('/admin/quiz/addquestion');
    	}
    	
    	if(isset($_POST['cmdDelQuiz']) && !empty($_POST['cb_listQuestion'])){
			$arrDel = $_POST['cb_listQuestion'];
			
			for($i=0; $i< count($arrDel); $i++){
				$this->questionTbl->deleteQuestion($arrDel[$i]);
			}
			
			$this->_redirect('admin/quiz/addquestion');
		}
    }

    public function importAction(){
    	if(isset($_POST['cmdQImport'])){
    		$fileName = WEB_ROOT.'/docs/'.$_FILES['fQImport']['name'];
    		
    		if ($_FILES['fQImport']['error'] > 0){
    			$this->jShowMessage("Hãy chọn file trước");
    			
    			echo '<script type="text/javascript">
    					$(document).ready(function(){
	    				 	$("#tabs-1").addClass("ui-tabs-hide");
				            $(".ui-state-active").removeClass("ui-tabs-selected ui-state-active");
				            
				            $(".tabs-2").addClass("ui-tabs-selected ui-state-active");            
				            $("#tabs-2").removeClass("ui-tabs-hide");
				        });
				       </script>			            
    			';
    			
    		}else{
	    		move_uploaded_file($_FILES['fQImport']['tmp_name'], $fileName );
	    		
	    		$dataFromExcel = $this->importFromExcel($fileName);
	    		$rowsNum = $dataFromExcel->rowcount();
	    		
	    		for($i=2; $i<=$rowsNum; $i++){
	    			$this->questionTbl
	    				->addQuestion(
	    						trim($dataFromExcel->val($i, 1)), 
	    						trim($dataFromExcel->val($i, 2)), 
	    						trim($dataFromExcel->val($i, 3)), 
	    						trim($dataFromExcel->val($i, 4)), 
	    						trim($dataFromExcel->val($i, 5)), 
	    						trim($dataFromExcel->val($i, 6)),
	    						trim($dataFromExcel->val($i, 7)),
	    						trim($dataFromExcel->val($i, 8))
	    				);
	    		}
	    		
	    		$this->jShowMessage("Nhập thành công!!!");
    		}
    		
    		
//    		$this->_redirect('/admin/lop/');
    	}
    	$this->_forward('index','question','admin');
    }
}









